package com.xy.Servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xy.dao.impl.PageResults;
import com.xy.model.Cell;
import com.xy.service.CellService;
import com.xy.service.impl.CellServiceImpl;

/**
 * Servlet implementation class CellServlet
 */
@WebServlet("/cellServlet")
public class CellServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CellServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		CellService service = new CellServiceImpl();

		if ("goup".equals(request.getParameter("status"))) {
			int id = Integer.valueOf(request.getParameter("id"));
			Cell cell = service.get(id);
			request.setAttribute("cell", cell);
			request.getRequestDispatcher("/pages/storage/storage006/shelf_modify.jsp").forward(request, response);

		} else if ("up".equals(request.getParameter("status"))) {
			int id = Integer.valueOf(request.getParameter("id"));
			Cell cell = service.get(id);
			cell.setName(request.getParameter("name"));
			cell.setRemark(request.getParameter("remark"));
			if(service.update(cell)) {
				request.getRequestDispatcher("/cellServlet?status=list&currentPage=0").forward(request, response);
			}else {
				System.out.println("error");
				request.setAttribute("cell", service.get(id));
				request.getRequestDispatcher("/pages/storage/storage006/shelf_modify.jsp").forward(request, response);
			}
		} else if ("add".equals(request.getParameter("status"))) {
			Cell cell = new Cell();
			cell.setName(request.getParameter("name"));
			cell.setRemark(request.getParameter("remark"));
			cell.setCreateDate(new Date());
			if (service.add(cell)) {
				request.getRequestDispatcher("/cellServlet?status=list&currentPage=0").forward(request, response);
			} else {
				System.out.println("error");
				request.getRequestDispatcher("/pages/storage/storage006/shelf_add.jsp").forward(request, response);
			}

		} else if ("list".equals(request.getParameter("status"))) {
			int currentPage = 0;
			if (request.getParameter("goalpage") == null) {
				currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
			} else
				currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

			PageResults pageResults = service.listByPage(currentPage);

			request.setAttribute("pageResults", pageResults);
			request.setAttribute("status", "list");
			request.setAttribute("list", null);

			request.getRequestDispatcher("/pages/storage/storage006/shelf_list.jsp").forward(request, response);

		} else if ("query".equals(request.getParameter("status"))) {
			int currentPage = 0;
			if (request.getParameter("goalpage") == null) {
				currentPage = Integer.valueOf(request.getParameter("currentPage")) - 1;
			} else
				currentPage = Integer.valueOf(request.getParameter("goalpage")) - 1;

			String name = null;
			String list = null;

			if (request.getParameter("list") == null) {
				name = request.getParameter("name") == null ? "%" : request.getParameter("name");
				list = name;
			} else {
				name = new String(request.getParameter("list").getBytes("iso-8859-1"), "utf-8");
				list = name;
			}

			PageResults pageResults = service.query(name, currentPage);

			request.setAttribute("pageResults", pageResults);
			request.setAttribute("status", "query");
			request.setAttribute("list", list);

			request.getRequestDispatcher("/pages/storage/storage006/shelf_list.jsp").forward(request, response);

		} else if ("del".equals(request.getParameter("status"))) {
			int id = Integer.valueOf(request.getParameter("id"));
			Cell cell = service.get(id);
			if(service.delete(cell)) {
				request.getRequestDispatcher("/cellServlet?status=list&currentPage=0").forward(request, response);
			}else {
				System.out.println("error");
				request.setAttribute("cell", service.get(id));
				request.getRequestDispatcher("/pages/public/main.jsp").forward(request, response);
			}
		}
	}

}
